package com.thinksns.sociax.component;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeUtils {

    /**
     * 把毫秒，转换成 ：。如：03:10
     *
     * @param millisencond
     * @return
     */
    public static String convertMilliSecondToMinute2(int millisencond) {
        int oneMinute = 1000 * 60;
        int minutes = millisencond / oneMinute;
        int sencond = (millisencond - minutes * oneMinute) / 1000;
        return getNum(minutes) + ":" + getNum(sencond);
    }

    /**
     * 把秒，转换成 ：。如：03:10
     *
     *
     * @param
     * @return
     */
    public static String convertSecondToMinute2(int seconds) {
        int oneMinute = 60;
        int minutes = seconds / oneMinute;
        int mSecond = seconds - minutes * oneMinute;
        return getNum(minutes) + ":" + getNum(mSecond);
    }

    public static String getNum(int num) {
        if (num >= 10) {
            return "" + num;
        } else {
            return "0" + num;
        }
    }

    public static String getTimestamp() {
        Timestamp ts = new Timestamp(System.currentTimeMillis());
        return String.valueOf(ts.getTime());
    }

    public static String getTimeTamp(long startTime, long endTime) {
        //怎么返回的是 ms, ... 请精确至ns好吗..
        startTime *= 1000;
        endTime *= 1000;
        SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy年MM月dd日");
        SimpleDateFormat dayFormat = new SimpleDateFormat("MM月dd日");
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
        SimpleDateFormat timeFormatNoSec = new SimpleDateFormat("HH:mm");
        StringBuffer returnStr = new StringBuffer();
        Date startDate = new Date(startTime);
        Date endDate = new Date(endTime);
        if (startDate.getYear() != new Date().getYear()) { //开始年份不是今年 加上年
            returnStr.append(yearFormat.format(endTime));
        } else { //是今年 只加上月日
            returnStr.append(dayFormat.format(startDate));
        }
        if (startDate.getSeconds() == 0) { // second为0时 省去
            returnStr.append(timeFormatNoSec.format(startTime));
        } else { // second不为0时 保留
            returnStr.append(timeFormat.format(startTime));
        }
        returnStr.append("~");
        if (startDate.getYear() != endDate.getYear()) { //结束年份不是开始年份 追加 年 月 日.
            returnStr.append(yearFormat.format(endTime));
        } else if (!dayFormat.format(startDate).equals(dayFormat.format(endDate))) { //年份相同 月日不一致 只追加月日
            returnStr.append(dayFormat.format(endTime));
        }
        //统一追加结束时间 hh:mm ? hh:mm:ss
        if (endDate.getSeconds() == 0) { // second为0时 省去
            returnStr.append(timeFormatNoSec.format(endTime));
        } else { // second不为0时 保留
            returnStr.append(timeFormat.format(endTime));
        }
        return returnStr.toString();
    }
    public static long getTime(String timeString,String format){
        long timeStamp = 0l;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date d;
        try{
            d = sdf.parse(timeString);
            timeStamp = d.getTime();
        } catch(ParseException e){
            e.printStackTrace();
        }
        return timeStamp;
    }

}
